<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理系统 - 权益明账通</title>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: #f5f7fa;
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 顶部导航栏 */
        .top-nav {
            background-color: #1976d2;
            color: white;
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .top-nav h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        /* 主要内容区域 */
        .main-container {
            display: flex;
            flex: 1;
        }

        /* 左侧导航栏 */
        .sidebar {
            width: 280px;
            background: white;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            padding: 1.5rem 0;
        }

        .user-info {
            padding: 0 1.5rem 1.5rem;
            border-bottom: 1px solid #eee;
            margin-bottom: 1rem;
        }

        .user-info h3 {
            color: #1976d2;
            margin-bottom: 0.5rem;
            font-size: 1.1rem;
        }

        .username-display {
            background: #f5f7fa;
            padding: 0.75rem;
            border-radius: 4px;
            font-weight: 500;
            color: #555;
        }

        .nav-section {
            margin-bottom: 1.5rem;
        }

        .nav-section h4 {
            padding: 0 1.5rem;
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .nav-item {
            display: block;
            padding: 0.75rem 1.5rem;
            color: #555;
            text-decoration: none;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }

        .nav-item:hover {
            background: #f5f7fa;
            color: #1976d2;
            border-left-color: #1976d2;
        }

        .nav-item.active {
            background: #e3f2fd;
            color: #1976d2;
            border-left-color: #1976d2;
            font-weight: 500;
        }

        /* 右侧内容区域 */
        .content {
            flex: 1;
            padding: 2rem;
            background: #f5f7fa;
        }

        .content-header {
            background: white;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .content-header h2 {
            color: #1976d2;
            margin-bottom: 0.5rem;
        }

        .content-body {
            background: white;
            border-radius: 8px;
            padding: 1.5rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
            min-height: 400px;
        }

        /* 视频管理样式 */
        .video-form {
            background: #f8f9fa;
            padding: 1.5rem;
            border-radius: 8px;
            margin-bottom: 2rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: #555;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
            transition: border-color 0.3s;
        }

        .form-control:focus {
            outline: none;
            border-color: #1976d2;
            box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.2);
        }

        .btn {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s;
        }

        .btn-primary {
            background: #1976d2;
            color: white;
        }

        .btn-primary:hover {
            background: #1565c0;
        }

        .video-list {
            display: grid;
            gap: 1.5rem;
        }

        .video-item {
            display: flex;
            align-items: center;
            background: #f8f9fa;
            border-radius: 8px;
            padding: 1rem;
            border: 1px solid #e9ecef;
        }

        .video-info {
            flex: 1;
        }

        .video-name {
            font-weight: 600;
            color: #1976d2;
            margin-bottom: 0.5rem;
        }

        .video-src {
            color: #666;
            font-size: 0.9rem;
            word-break: break-all;
        }

        .video-actions {
            display: flex;
            gap: 0.5rem;
        }

        .btn-edit {
            background: #ff9800;
            color: white;
        }

        .btn-edit:hover {
            background: #f57c00;
        }

        .btn-delete {
            background: #f44336;
            color: white;
        }

        .btn-delete:hover {
            background: #d32f2f;
        }

        .message {
            padding: 1rem;
            border-radius: 4px;
            margin-bottom: 1rem;
            display: none;
        }

        .message.success {
            background: #e8f5e9;
            color: #4caf50;
            border: 1px solid #c8e6c9;
        }

        .message.error {
            background: #ffebee;
            color: #f44336;
            border: 1px solid #ffcdd2;
        }

        /* 加载状态 */
        .loading {
            text-align: center;
            padding: 2rem;
            color: #666;
        }

        .loading::after {
            content: '...';
            animation: dots 1.5s steps(4, end) infinite;
        }

        @keyframes dots {
            0%, 20% { color: rgba(0,0,0,0); text-shadow: .25em 0 0 rgba(0,0,0,0), .5em 0 0 rgba(0,0,0,0); }
            40% { color: #666; text-shadow: .25em 0 0 rgba(0,0,0,0), .5em 0 0 rgba(0,0,0,0); }
            60% { text-shadow: .25em 0 0 #666, .5em 0 0 rgba(0,0,0,0); }
            80%, 100% { text-shadow: .25em 0 0 #666, .5em 0 0 #666; }
        }

        /* 移动端适配 */
        @media (max-width: 768px) {
            .main-container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
                padding: 1rem 0;
            }

            .top-nav {
                padding: 1rem;
            }

            .content {
                padding: 1rem;
            }

            .nav-items {
                display: flex;
                overflow-x: auto;
            }

            .nav-item {
                white-space: nowrap;
                border-left: none;
                border-bottom: 3px solid transparent;
            }

            .nav-item.active {
                border-left: none;
                border-bottom-color: #1976d2;
            }

            .video-item {
                flex-direction: column;
                align-items: flex-start;
            }

            .video-actions {
                margin-top: 1rem;
                width: 100%;
                justify-content: flex-end;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<nav class="top-nav">
    <h1>权益明账通 - 后台管理系统</h1>
    <button class="logout-btn" id="logoutBtn">退出登录</button>
</nav>

<!-- 主要内容区域 -->
<div class="main-container">
    <!-- 左侧导航栏 -->
    <aside class="sidebar">
        <div class="user-info">
            <h3>当前用户</h3>
            <div class="username-display" id="usernameDisplay">加载中...</div>
        </div>

        <div class="nav-section">
            <h4>内容管理</h4>
            <a href="#" class="nav-item active" data-section="video">视频管理</a>
            <a href="#" class="nav-item" data-section="volunteer">志愿者管理</a>
        </div>
    </aside>

    <!-- 右侧内容区域 -->
    <main class="content">
        <div class="content-header">
            <h2 id="contentTitle">视频管理</h2>
            <p>管理平台上的视频内容和相关信息</p>
        </div>

        <div class="content-body" id="contentBody">
            <div class="loading">系统验证中</div>
        </div>
    </main>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 获取DOM元素
        const usernameDisplay = document.getElementById('usernameDisplay');
        const contentTitle = document.getElementById('contentTitle');
        const contentBody = document.getElementById('contentBody');
        const logoutBtn = document.getElementById('logoutBtn');
        const navItems = document.querySelectorAll('.nav-item');

        // 从cookie获取用户信息
        function getCookie(name) {
            const cookies = document.cookie.split(';');
            for (let cookie of cookies) {
                const [cookieName, cookieValue] = cookie.trim().split('=');
                if (cookieName === name) {
                    return decodeURIComponent(cookieValue);
                }
            }
            return null;
        }

        // 验证登录状态
        async function verifyLogin() {
            try {
                // 从cookie获取认证信息
                const username = getCookie('username') || 'admin';
                const password = getCookie('password') || 'admin123';
                const authid = getCookie('authid') || '0';
                const authtoken = getCookie('authtoken') || '0';

                // 发送验证请求
                const response = await fetch('/api/admin/verify', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password,
                        authid: authid,
                        authtoken: authtoken
                    })
                });

                if (!response.ok) {
                    throw new Error('HTTP错误: ' + response.status);
                }

                const result = await response.json();

                if (result.code === 200) {
                    // 登录成功，显示用户名
                    usernameDisplay.textContent = username;
                    // 自动加载视频管理页面
                    loadVideoManagement();
                } else {
                    // 登录失败，跳转到登录页
                    window.location.href = './login.html';
                }
            } catch (error) {
                console.error('验证登录失败:', error);
                window.location.href = './login.html';
            }
        }

        // 加载视频管理页面
        async function loadVideoManagement() {
            contentBody.innerHTML = `
                    <div id="message" class="message"></div>
                    <div class="video-form">
                        <h3>添加已有视频到网站</h3>
                        <form id="addVideoForm">
                            <div class="form-group">
                                <label for="videoName">视频名称</label>
                                <input type="text" id="videoName" class="form-control" placeholder="请输入视频名称" required>
                            </div>
                            <div class="form-group">
                                <label for="videoSrc">视频路径</label>
                                <input type="text" id="videoSrc" class="form-control" placeholder="请输入视频路径" required>
                            </div>
                            <button type="submit" class="btn btn-primary">添加视频</button>
                        </form>
                    </div>
                    <div class="video-list-container">
                        <h3>视频列表</h3>
                        <div id="videoList" class="video-list">
                            <div class="loading">加载视频数据中...</div>
                        </div>
                    </div>
                `;

            // 绑定表单提交事件
            document.getElementById('addVideoForm').addEventListener('submit', handleAddVideo);

            // 加载视频列表
            await loadVideos();
        }

        // 加载视频列表
        async function loadVideos() {
            try {
                const videos = await fetchVideos();
                displayVideos(videos);
            } catch (error) {
                console.error('加载视频失败:', error);
                document.getElementById('videoList').innerHTML = '<div class="message error">加载视频失败，请刷新页面重试</div>';
            }
        }

        // 获取视频列表
        async function fetchVideos() {
            const response = await fetch('/api/videos');
            const result = await response.json();

            if (result.code === 200) {
                return result.data || [];
            } else {
                throw new Error(result.msg || '获取视频列表失败');
            }
        }

        // 显示视频列表
        function displayVideos(videos) {
            const videoList = document.getElementById('videoList');

            if (!videos || videos.length === 0) {
                videoList.innerHTML = '<div class="message">暂无视频数据</div>';
                return;
            }

            videoList.innerHTML = videos.map(video => `
                    <div class="video-item" data-id="${video.id}">
                        <div class="video-info">
                            <div class="video-name">${video.name}</div>
                            <div class="video-src">${video.video_src}</div>
                        </div>
                        <div class="video-actions">
                            <button class="btn btn-edit" onclick="editVideo(${video.id}, '${video.name}', '${video.video_src}')">修改</button>
                            <button class="btn btn-delete" onclick="deleteVideo(${video.id})">删除</button>
                        </div>
                    </div>
                `).join('');
        }

        // 添加视频
        async function handleAddVideo(e) {
            e.preventDefault();

            const name = document.getElementById('videoName').value.trim();
            const video_src = document.getElementById('videoSrc').value.trim();

            if (!name || !video_src) {
                showMessage('请填写完整的视频信息', 'error');
                return;
            }

            try {
                const result = await addVideo(name, video_src);
                if (result.code === 200) {
                    showMessage('视频添加成功，页面即将刷新...', 'success');
                    // 1秒后刷新页面
                    setTimeout(() => {
                        location.reload();
                    }, 1000);
                }
            } catch (error) {
                showMessage('添加视频失败: ' + error.message, 'error');
            }
        }

        // 添加视频API
        async function addVideo(name, video_src) {
            const response = await fetch('/api/videos', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    name: name,
                    video_src: video_src
                })
            });

            return await response.json();
        }

        // 修改视频
        window.editVideo = async function(id, currentName, currentSrc) {
            const newName = prompt('请输入新的视频名称:', currentName);
            if (newName === null) return;

            const newSrc = prompt('请输入新的视频路径:', currentSrc);
            if (newSrc === null) return;

            try {
                const result = await updateVideo(id, newName, newSrc);
                if (result.code === 200) {
                    showMessage('视频修改成功，页面即将刷新...', 'success');
                    // 1秒后刷新页面
                    setTimeout(() => {
                        location.reload();
                    }, 1000);
                }
            } catch (error) {
                showMessage('修改视频失败: ' + error.message, 'error');
            }
        }

        // 修改视频API
        async function updateVideo(id, name, video_src) {
            const response = await fetch(`/api/videos/${id}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    name: name,
                    video_src: video_src
                })
            });

            return await response.json();
        }

        // 删除视频
        window.deleteVideo = async function(id) {
            if (!confirm('确定要删除这个视频吗？')) return;

            try {
                const result = await deleteVideoAPI(id);
                if (result.code === 200) {
                    showMessage('视频删除成功，页面即将刷新...', 'success');
                    // 1秒后刷新页面
                    setTimeout(() => {
                        location.reload();
                    }, 1000);
                }
            } catch (error) {
                showMessage('删除视频失败: ' + error.message, 'error');
            }
        }

        // 删除视频API
        async function deleteVideoAPI(id) {
            const response = await fetch(`/api/videos/${id}`, {
                method: 'DELETE'
            });

            return await response.json();
        }

        // 显示消息
        function showMessage(text, type) {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
        }

        // 显示内容区域
        function showContent(section) {
            // 更新导航激活状态
            navItems.forEach(item => {
                if (item.getAttribute('data-section') === section) {
                    item.classList.add('active');
                } else {
                    item.classList.remove('active');
                }
            });

            // 更新内容标题和内容
            if (section === 'video') {
                contentTitle.textContent = '视频管理';
                loadVideoManagement();
            } else if (section === 'volunteer') {
                contentTitle.textContent = '志愿者管理';
                contentBody.innerHTML = `
                        <div class="welcome-message">
                            <h3>志愿者管理功能</h3>
                            <p>志愿者管理功能预留区域</p>
                        </div>
                    `;
            }
        }

        // 导航项点击事件
        navItems.forEach(item => {
            item.addEventListener('click', function(e) {
                e.preventDefault();
                const section = this.getAttribute('data-section');
                showContent(section);
            });
        });

        // 退出登录
        logoutBtn.addEventListener('click', function() {
            // 清除cookie
            document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
            document.cookie = "password=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
            document.cookie = "authid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
            document.cookie = "authtoken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

            // 跳转到登录页
            window.location.href = './login.html';
        });

        // 页面加载时验证登录状态
        verifyLogin();
    });
</script>
</body>
</html>